package com.sttg.csp.registration.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import com.sttg.csp.common.exception.BizException;
import com.sttg.csp.common.util.DBUtil;
import com.sttg.csp.common.util.DateTimeUtil;
import com.sttg.csp.common.util.LDAPUtil;
import com.sttg.csp.common.util.RandomGUID;
import com.sttg.csp.common.util.paged.PagedList;

public class LogManageDAO {

	private static final LogManageDAO instance = new LogManageDAO();   
	
	/** 
	 * Private the default constructor.
	 */   
	private LogManageDAO() {}    
   
	/** 
	 * Static factory methods.
	 */   
	public static LogManageDAO getInstance() {
		return instance;    
	}    
	
	public void queryLogPagedList(Map param, PagedList pagedRecords) throws BizException  {
		List<Object> list = new ArrayList<Object>();
		
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select LOG_ID,ACTION_TYPE,ACTION_BY,ACTION_FOR,ACTION_TIME from ACTION_LOG order by ACTION_TIME desc limit " + (pagedRecords.getPageNum() - 1) * pagedRecords.getPageSize() + "," + pagedRecords.getPageSize());
			while (rs.next()) {
				Map<String, Object> each = new HashMap<String, Object>();
				String id = rs.getString("LOG_ID");
				String actionType = rs.getString("ACTION_TYPE");
				String actionBy = rs.getString("ACTION_BY");
				String actionFor = rs.getString("ACTION_FOR");
				String actionTime = rs.getString("ACTION_TIME");
				each.put("LOG_ID", id);
				each.put("ACTION_TYPE", actionType);
				each.put("ACTION_BY", actionBy);
				each.put("ACTION_FOR", actionFor);
				each.put("ACTION_TIME", actionTime);
				list.add(each);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, stmt, con);
		}
		System.out.println(list);
		pagedRecords.setTotalNum(queryLogCount(param));
		pagedRecords.setSource(list) ;
	}
	
	public int queryLogCount(Map param) throws BizException  {
		int count = 0;
		
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select count(1) from ACTION_LOG ");
			while (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, stmt, con);
		}
		return count;
	}

	/**
	 * insert a Log info
	 * @param param
	 * @throws BizException
	 */
	public void doInsertLog(Map param) throws BizException {
		
		String actionType = (String) param.get("ACTION_TYPE");
		String actionBy = (String) param.get("ACTION_BY");
		String actionFor = (String) param.get("ACTION_FOR");
		
		String sql = "insert into action_log(LOG_ID,ACTION_TYPE,ACTION_BY,ACTION_FOR,ACTION_TIME) values(" +
			"'" + RandomGUID.getRandomGuid() +
			"', '" + actionType +
			"', '" +  actionBy +
			"', '" +  actionFor +
			"', '" + DateTimeUtil.getDateTime()  + 
			"')";
		System.out.println(sql);
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			stmt.executeUpdate(sql) ;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, stmt, con);
		}
	}
	/**
	 * Delete a Log info.
	 * @param param
	 * @throws BizException
	 */
	public void doDeleteLog(Map param) throws BizException {
	}
	
	public static void main(String[] args) {
		try {
		} catch (Exception e) {
			System.out.println(e);
		}
	}

}